package com.ruoyi.service;
import com.google.gson.Gson;
import com.ruoyi.domain.BxoyData;
import com.ruoyi.domain.DeviceReport;
import com.ruoyi.domain.HrData;
import com.ruoyi.domain.alarm.DeviceAIarm;
import com.ruoyi.utils.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@Component
public class SaveInfoToRedisService {

    private static final Logger log = LoggerFactory.getLogger(SaveInfoToRedisService.class);
    // 设备id列表
    private static final String DEVICE_IDS_KEY = "HEALTH_WEARABLE_DEVICEIDS";
    // 上传的数据
    private static final String REPORT_KEY = "HEALTH_WEARABLE_REPORT_";
    private static final String ALARM_KEY = "HEALTH_WEARABLE_ALARM_";
    private static final Gson gson = new Gson();
    // 心率
// 3 usages
    private static final String HR_KEY = "HEALTH_WEARABLE_HR_";
    // 血氧
// 3 usages
    private static final String BXOY_KEY = "HEALTH_WEARABLE_BXOY_";

    @Autowired
    private RedisCache redisCache;

    public void saveDeviceIdsToRedis(String deviceId) {
        try {
            if (StringUtils.isBlank(deviceId)) {
                log.info("saveDeviceIdsToRedis deviceId is null:{},skip...", deviceId);
                return;
            }
            log.info("start saveDeviceIdsToRedis deviceId:{}", deviceId);
            if (redisCache.hasKey(DEVICE_IDS_KEY)) {
                log.info("exist key:{}", DEVICE_IDS_KEY);
                redisCache.addCacheSet(DEVICE_IDS_KEY, deviceId);
            } else {
                Set<String> set = new HashSet<>();
                set.add(deviceId);
                redisCache.setCacheSet(DEVICE_IDS_KEY, set);
            }
        } catch (Exception e) {
            log.error("saveDeviceIdsToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }

    public void saveDeviceReportToRedis(String deviceId, DeviceReport report) {
        try {
            if (report == null) {
                log.info("deviceId:{} saveDeviceReportToRedis report is null:{},skip...", deviceId, report);
                return;
            }
            log.info("deviceId:{} start saveDeviceReportToRedis DeviceReport:{}",
                    deviceId, gson.toJson(report));
            if (redisCache.hasKey(REPORT_KEY + deviceId)) {
                redisCache.addCacheList(REPORT_KEY + deviceId, report);
            } else {
                redisCache.setCacheList(REPORT_KEY + deviceId, Collections.singletonList(report));
            }
        } catch (Exception e) {
            log.error("saveDeviceReportToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }
    public void saveAlarmToRedis(String deviceId, DeviceAIarm alarm) {
        try {
            if (alarm == null) {
                log.info("deviceId:{} start saveAlarmToRedis alarm is null:{}, skip...", deviceId, alarm);
                return;
            }
            log.info("deviceId:{} start saveAlarmToRedis alarm:{}", deviceId, gson.toJson(alarm));
            if (redisCache.hasKey(ALARM_KEY + deviceId)) {
                redisCache.addCacheList(ALARM_KEY + deviceId, alarm);
            } else {
                redisCache.setCacheList(ALARM_KEY + deviceId, Collections.singletonList(alarm));
            }
        } catch (Exception e) {
            log.error("saveAlarmToRedis error, deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }
    public void saveHrToRedis(String deviceId, HrData hr) {
        try {
            if (hr == null) {
                log.info("deviceId:{} start saveHrToRedis hr is null:{},skip...", deviceId, hr);
                return;
            }
            log.info("deviceId:{} start saveHrToRedis hr:{}", deviceId, gson.toJson(hr));
            if (redisCache.hasKey(HR_KEY + deviceId)) {
                redisCache.addCacheList(HR_KEY + deviceId, hr);
            } else {
                redisCache.setCacheList(HR_KEY + deviceId, Collections.singletonList(hr));
            }
        } catch (Exception e) {
            log.error("saveHrToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }
    public void saveBxoyToRedis(String deviceId, BxoyData bxoy) {
        try {
            if (bxoy == null) {
                log.info("deviceId:{} start saveBxoyToRedis bxoy is null:{},skip...", deviceId, bxoy);
                return;
            }
            log.info("deviceId:{} start saveBxoyToRedis bxoy:{}", deviceId, gson.toJson(bxoy));
            if (redisCache.hasKey(BXOY_KEY + deviceId)) {
                redisCache.addCacheList(BXOY_KEY + deviceId, bxoy);
            } else {
                redisCache.setCacheList(BXOY_KEY + deviceId, Collections.singletonList(bxoy));
            }
        } catch (Exception e) {
            log.error("saveBxoyToRedis error,deviceId:{} msg:{}", deviceId, e.getMessage());
        }
    }


}